/**
 * Copyright 2022 Stars embedded, All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 * @FilePath: /FA/SmartCentControl/entry/src/main/js/default/pages/index/index.js
 * @Description:
 * @Author: lin zhang
 * @Date: 2022-03-04 18:55:47
 * @LastEditTime: 2022-03-15 15:35:18
 * @LastEditors: lin zhang
 * 变更历史:
 */
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include "hdf_sbuf.h"
#include "hdf_io_service_if.h"

#define MQTT_SERVICE "hdf_scp"

static int SendEvent(struct HdfIoService *serv, uint8_t eventData)
{
    int ret = 0;
    struct HdfSBuf *data = HdfSBufObtainDefaultSize();
    if (data == NULL)
    {
        printf("fail to obtain sbuf data!\r\n");
        return 1;
    }

    struct HdfSBuf *reply = HdfSBufObtainDefaultSize();
    if (reply == NULL)
    {
        printf("fail to obtain sbuf reply!\r\n");
        ret = HDF_DEV_ERR_NO_MEMORY;
        goto out;
    }
    /* 写入数据 */
    if (!HdfSbufWriteUint8(data, eventData))
    {
        printf("fail to write sbuf!\r\n");
        ret = HDF_FAILURE;
        goto out;
    }
    /* 通过Dispatch发送到驱动 */
    ret = serv->dispatcher->Dispatch(&serv->object, eventData, data, reply);
    if (ret != HDF_SUCCESS)
    {   
        goto out;
    }

out:
    HdfSBufRecycle(data);
    HdfSBufRecycle(reply);
    return ret;
}

int main(int argc, char **argv)
{   
    /* 获取服务 */
    struct HdfIoService *serv = HdfIoServiceBind(MQTT_SERVICE);
    if (serv == NULL)
    {
        printf("fail to get service %s!\r\n", MQTT_SERVICE);
        return HDF_FAILURE;
    }


    SendEvent(serv, 0);
    sleep(5);
    SendEvent(serv, 1);
    // HdfIoServiceRecycle(serv);
    printf("\n exit");
    return HDF_SUCCESS;
}